package cn.bdqn.controller;

import cn.bdqn.pojo.Integral;
import cn.bdqn.service.SignService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author 小涛
 * @date: 2020/7/18 8:24
 */
@Controller
public class SignController {

    @Autowired
    SignService signService;

    @GetMapping("/emp/sign")
    @ResponseBody
    public String sign(Integral integral, HttpServletRequest request){
        Integer id = (Integer) request.getSession().getAttribute("id");
//        int staffid = 2;
        int addintegral=5;
        String describe = "签到增加5积分";
        List<Integral> integrals = signService.getIntegralAll1(id,describe);
        for (Integral integral1 : integrals) {
            System.out.println(integral1.getTime());
        }

        return JSON.toJSONString(integrals);
    }

    @PostMapping("/addSign")
    public String addSign(Integral integral, HttpServletRequest request){
        Integer employessid = (Integer) request.getSession().getAttribute("id");
//        integral.setAddintegral(5);
//        integral.setTime(new Date());
//        int staffid = id;
        int addintegral=5;

        Date createtime=new Date();
        String describe = "签到增加5积分";

        int count = signService.addIntegralAll1(employessid,describe,createtime);

        return "index";

    }

    @ResponseBody
    @GetMapping("/signtime")
    public String signtime(Integral integral, HttpServletRequest request) throws ParseException {
        Calendar calendar1 = Calendar.getInstance();

        Integer employessid = (Integer) request.getSession().getAttribute("id");

        Date time = null;

        String describe = "签到增加5积分";
        List<Integral> integrals = signService.getIntegralAll1(employessid,describe);
        System.out.println(integrals);
        for (Integral integral1 : integrals) {
            //System.out.println(integral1.getTime());
            time=integral1.getCreatetime();
            System.out.println(integral1.getCreatetime());
            System.out.println(time);
        }
        System.out.println(time);

        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", java.util.Locale.US);
        Date date = sdf.parse(time.toString());
        time=new java.sql.Date(date.getTime());
        System.out.println(time);

        System.out.println(time.getDate());

        System.out.println(calendar1.get(Calendar.DAY_OF_MONTH));

        int count = 0;
        if (time.getDate()==calendar1.get(Calendar.DAY_OF_MONTH)){
            count=1;
        }else {
            count=0;
        }
        return JSON.toJSONString(count);
    }

}
